//
//  MainViewController.swift
//  手账
//
//  Created by zhuxiaoxiang on 2018/12/15.
//  Copyright © 2018年 zhuxiaoxiang. All rights reserved.
//

import UIKit

class MainViewController: BaseViewController {

    fileprivate lazy var categoryArray = ["餐饮", "购物", "日用", "交通", "数码", "电影", "烟酒", "旅行", "更多"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func config() {
        super.config()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.itemSize = CGSize(width: collectionView.frame.size.width / 3, height: collectionView.frame.size.height / 3)
        flowLayout.sectionInset = UIEdgeInsets.zero
        collectionView.collectionViewLayout = flowLayout
        collectionView.register(UINib(nibName: "MainCell", bundle: nil), forCellWithReuseIdentifier: "CELL")
    }
    
    @IBOutlet weak var collectionView: UICollectionView!
    
}

extension MainViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: false)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    @IBAction func startKeepingBook(_ sender: UIButton) {
//        if AppUser.sharedInstance.loginState == .logout {
//            AppUser.presentLoginVC()
//        }else {
//            let keepBookVC = KeepBookViewController()
//            self.navigationController?.pushViewController(keepBookVC, animated: true)
//        }
        let docVC = UIDocumentPickerViewController.init(documentTypes: ["public.image", "com.adobe.pdf"], in: .open)
        docVC.delegate = self
        navigationController?.present(docVC, animated: true, completion: nil)
    }
}

extension MainViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 9
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CELL", for: indexPath) as! MainCell
        cell.categoryLB.text = self.categoryArray[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == 8 {
            self.navigationController?.pushViewController(BookViewController(), animated: true)
        }else {
            if AppUser.sharedInstance.loginState == .logout {
                AppUser.presentLoginVC()
            }else {
                self.navigationController?.pushViewController(BookListViewController(), animated: true)
            }
        }
    }
}

extension MainViewController: UIDocumentPickerDelegate {
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        print(urls)
    }
}
